<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>任务指派信息</span>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['ehr:assignment:add']">新增任务指派</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="assignmentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="任务编号" align="center" prop="workEffortId" /> -->
        <!-- <el-table-column label="受托人" align="center" prop="partyId" /> -->
        <el-table-column label="受托人" align="center" prop="partyId" :formatter="partyIdFormat" />
        <el-table-column label="任务" align="center" prop="workEffortId" :formatter="workEffortIdFormat" />
        <!-- <el-table-column label="受托人角色" align="center" prop="roleTypeId" /> -->
        <el-table-column label="开始时间" align="center" prop="fromDate" width="180" />
        <el-table-column label="结束时间" align="center" prop="thruDate" width="180" />
        <!-- <el-table-column label="状态更新时间" align="center" prop="statusDateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statusDateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
        <el-table-column label="委派原因" align="center" prop="delegateReasonEnumId" />
        <el-table-column label="备注" align="center" prop="comments" />
        <!-- <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
              v-hasPermi="['ehr:assignment:detail']">详情</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['ehr:assignment:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['ehr:assignment:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-card>
    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" /> -->

    <!-- 添加或修改任务指派对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务" prop="workEffortId">
              <span>{{ taskName }}</span>
              <!-- <el-select v-model="form.workEffortId" placeholder="请选择任务" clearable filterable>
                <el-option v-for="dict in workEffortIdOptions" :key="dict.workEffortId" :label="dict.workEffortName"
                  :value="dict.workEffortId" />
              </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工" prop="partyId">
              <el-select v-model="form.partyId" placeholder="请选择员工">
                <el-option v-for="dict in partyIdOptions" :key="dict.partyId" :label="dict.firstName"
                  :value="dict.partyId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="fromDate">
              <el-date-picker clearable v-model="form.fromDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="thruDate">
              <el-date-picker clearable v-model="form.thruDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="委派原因" prop="delegateReasonEnumId">
              <el-input v-model="form.delegateReasonEnumId" placeholder="请输入委派原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="备注" prop="comments">
              <el-input v-model="form.comments" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="statusId">
              <el-select v-model="form.statusId" placeholder="请选择状态">
                <el-option v-for="dict in statusIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssignment, getAssignment, delAssignment, addAssignment, updateAssignment } from "@/api/ehr/assignment";
import { getTask } from "@/api/ehr/task";
export default {
  name: "Card_Assignment",
  props: {
    taskId: {
      type: String
    },
  },
  data() {
    return {
      taskName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务指派表格数据
      assignmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 任务编号业务字典
      workEffortIdOptions: [],
      // 受托人业务字典
      partyIdOptions: [],
      // 状态业务字典
      statusIdOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        workEffortId: null,
        partyId: null,
        fromDate: null,
        thruDate: null,
      },
      // 任务信息表单参数
      formTask: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workEffortId: [
          { required: true, message: "任务编号不能为空", trigger: "change" }
        ],
        partyId: [
          { required: true, message: "受托人不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    // 任务编号业务字典
    this.getEntityDicts("WorkEffort:workEffortId:workEffortName").then((response) => {
      this.workEffortIdOptions = response.data;
    });
    // 受托人业务字典
    this.getEntityDicts("Person:partyId:firstName").then((response) => {
      this.partyIdOptions = response.data;
    });
    // 状态业务字典
    this.getEnumDicts("ACTIVE_STOP").then((response) => {
      this.statusIdOptions = response.data;
    });
    // 获取任务信息
    getTask(this.taskId).then(response => {
      this.formTask = response.data;
      this.taskName = response.data.workEffortName;
    });

    // 查询当前任务下的 任务指派数据
    this.queryParams.workEffortId = this.taskId;
    this.getList();
  },
  methods: {
    workEffortIdFormat(row, column) {
      let val = null;
      this.workEffortIdOptions.forEach((item, index) => {
        if (row.workEffortId === item.workEffortId) {
          val = item.workEffortName;
        }
      });
      return val;
    },
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    statusIdFormat(row, column) {
      let val = null;
      this.statusIdOptions.forEach((item, index) => {
        if (row.statusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询任务指派列表 */
    getList() {
      this.loading = true;
      listAssignment(this.queryParams).then(response => {
        this.assignmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workEffortId: null,
        partyId: null,
        roleTypeId: null,
        fromDate: null,
        thruDate: null,
        assignedByUserLoginId: null,
        statusId: null,
        statusDateTime: null,
        expectationEnumId: null,
        delegateReasonEnumId: null,
        facilityId: null,
        comments: null,
        mustRsvp: null,
        availabilityStatusId: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workEffortId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      // 任务ID默认赋值
      this.form.workEffortId = this.taskId;
      
      this.open = true;
      this.title = "添加任务指派";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      // const workEffortId = row.workEffortId || this.ids
      getAssignment(row).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "任务指派详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const workEffortId = row.workEffortId || this.ids
      getAssignment(row).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务指派";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.partyIdFormat != null && this.updateFlag) {
          //   updateAssignment(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          addAssignment(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });

          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const party = row.workEffortId || this.ids;
      this.$modal.confirm('是否确认删除任务指派人员为"' + row.partyId + '"的数据项？').then(function () {
        return delAssignment(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/assignment/export', {
        ...this.queryParams
      }, `assignment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
